package cn.tedu.em.maintain.webapi.controller;

import cn.tedu.em.common.pojo.maintain.dto.MaintainAddNewDTO;
import cn.tedu.em.common.pojo.maintain.dto.MaintainUpdateDTO;
import cn.tedu.em.common.pojo.maintain.vo.MaintainListItemVO;
import cn.tedu.em.common.pojo.maintain.vo.MaintainStandardVO;
import cn.tedu.em.common.restful.JsonResult;
import cn.tedu.em.maintain.service.IMaintainService;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/maintains")
@Api(tags = "维修管理模块")
public class MaintainController {

    @Autowired
    private IMaintainService maintainService;

    // http://localhost:30002/maintains/add-new
    @ApiOperation("添加维修")
    @ApiOperationSupport(order = 100)
    @PostMapping("/add-new")
    public JsonResult addNew(MaintainAddNewDTO maintainAddNewDTO){
        log.info("开始处理【添加维修】的请求，参数：{}", maintainAddNewDTO);
        maintainService.addNew(maintainAddNewDTO);
        String message = "提交维修成功!";
        return JsonResult.ok(message);
    }

    // http://localhost:30002/maintains/9527/delete
    @ApiOperation("根据ID删除维修")
    @ApiOperationSupport(order = 200)
    @PostMapping("/{id:[0-9]+}/delete")
    public JsonResult delete(@PathVariable Long id){
        log.debug("开始处理【根据ID删除维修】的请求，参数：{}", id);
        maintainService.delete(id);
        return JsonResult.ok("删除成功!");
    }

    // http://localhost:9080/maintains/9527/dispose
    @PostMapping("/{id:[0-9]+}/dispose")
    @ApiOperation("已处理维修")
    @ApiOperationSupport(order = 310)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> SetDispose(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【维修状态】的请求，参数：{}", id);
        maintainService.SetDispose(id);
        return JsonResult.ok();
    }

    // http://localhost:9080/maintains/9527/undispose
    @PostMapping("/{id:[0-9]+}/undispose")
    @ApiOperation("未处理维修")
    @ApiOperationSupport(order = 311)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "类别ID", required = true, dataType = "long")
    })
    public JsonResult<Void> SetUndispose(@PathVariable @Range(min = 1, message = "请提交有效的ID值！") Long id) {
        log.debug("开始处理【维修状态】的请求，参数：{}", id);
        maintainService.SetUndispose(id);
        return JsonResult.ok();
    }

    // http://localhost:30002/maintains/9527/update
    @ApiOperation("修改维修详情")
    @ApiOperationSupport(order = 300)
    @PostMapping("/{id:[0-9]+}/update")
    public JsonResult update(@PathVariable Long id, MaintainUpdateDTO maintainUpdateDTO){
        log.debug("开始处理【修改维修详情】的请求，参数ID：{}, 新数据：{}", id, maintainUpdateDTO);
        maintainService.updateInfoById(id, maintainUpdateDTO);
        return JsonResult.ok("修改成功!");
    }

    // http://localhost:30002/maintains/9527
    @ApiOperation("根据ID查询维修详情")
    @ApiOperationSupport(order = 400)
    @GetMapping("/{id:[0-9]+}")
    public JsonResult<MaintainStandardVO> getStandardById(@PathVariable Long id){
        log.debug("开始处理【根据ID查询维修详情】的请求，参数：{}", id);
        MaintainStandardVO maintain = maintainService.getStandardById(id);
        return JsonResult.ok(maintain);
    }

    // http://localhost:30002/maintains
    @ApiOperation("查询维修列表")
    @ApiOperationSupport(order = 420)
    @GetMapping("")
    public JsonResult<List<MaintainListItemVO>> list(){
        log.debug("开始处理【查询维修列表】的请求，无参数");
        List<MaintainListItemVO> list = maintainService.list();
        return JsonResult.ok(list);
    }


}
